package com.ftg.learn.sercice.impl;

import com.ftg.learn.dto.SalePart;
import com.ftg.learn.entity.SaleEntity;
import com.ftg.learn.mapper.SaleMapper;
import com.ftg.learn.sercice.ISaleService;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 业务逻辑 ： Service
 *
 * ioc : 容器 注入 查找
 * @author wangty
 * @date 2022/10/18
 */
@Service
public class SaleServiceImpl implements ISaleService {

    @Resource
    SaleMapper saleMapper;

    /**
     * 查询全部商品信息
     * @return
     */
    @Override
    public List<SaleEntity> showAll() {
        return saleMapper.showAll();
    }

    /**
     * 模糊查询带一个参数
     * @param name 商品名称
     * @return
     */
    @Override
    public List<SaleEntity> likeWithName(String name) {
        if (Objects.isNull(name)){
            throw new RuntimeException("对不起，请输入正确的查询实例");
        }
        return saleMapper.likeWithName(name);
    }

    /**
     * 动态模糊查询（可填可不填）
     * @param name 商品名称
     * @param address 商品地址
     * @return
     */
    @Override
    public List<SaleEntity> likeWithNameAddress(String name, String address) {
        return saleMapper.likeWithNameAddress(name,address);
    }

    /**
     * 分页查询
     * @param page 页数
     * @param limit 一页显示条数
     * @param name 商品名称
     * @param address 商品地址
     * @return
     */
    @Override
    public Map<String, Object> pageWithNameAddress(Integer page, Integer limit, String name, String address) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("page",page);
        map.put("limit",limit);
        int start = page *limit - limit;
        List<SaleEntity> list = saleMapper.pageWithNameAddress(start,limit,name,address);
        map.put("data",list);
        int count = saleMapper.pageWithNameAddressCount(name,address);
        map.put("total",count);
        return map;
    }

    /**
     * 按照实例添加
     * @param saleEntity 实例
     * @return
     */
    @Override
    public int addEntity(SaleEntity saleEntity) {
        return saleMapper.addEntity(saleEntity);
    }

    /**
     * 按照dto进行添加
     * @param salePart 部分重要属性
     * @return
     */
    @Override
    public int addDto(SalePart salePart) {
        return saleMapper.addDto(salePart);
    }

    /**
     * 按照实例进行修改
     * @param saleEntity 实例
     * @return
     */
    @Override
    public int updateSaleEntity(
            @ApiParam(value = "updateSaleEntity") @RequestBody SaleEntity saleEntity) {
        return saleMapper.updateSaleEntity(saleEntity);
    }

    /**
     * 按照主键进行修改
     * @param saleEntity 实例
     * @param id 主键
     * @return
     */
    @Override
    public int updateSaleEntityWithId(
            @ApiParam(value = "updateSaleEntity") @RequestBody SaleEntity saleEntity,
            @RequestParam(value = "id") @ApiParam(value = "主键") Integer id) {
        return saleMapper.updateSaleEntityWithId(saleEntity,id);
    }

    /**
     * 按照主键进行删除
     * @param id 主键
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return saleMapper.deleteById(id);
    }

    /**
     * 逻辑删除
     * @param id 主键
     * @param flag 判断
     * @return
     */
    @Override
    public int deleteByIdLogic(Integer id, Integer flag) {
        return saleMapper.deleteByIdLogic(id,flag);
    }


}
